/*
 * Name: DateActivity
 * Author: Sam Oakley
 * Date: 24/3/2010
 * Version Number: 1.2
 * Description: Displays contact information. Has button to open email client. 
 * Changes History: 
 * 1.0 Created class
 * 1.1 Changed date formatting
 * 1.2 Added hiding of null data
 */

package com.blork.rag;

import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class DateActivity extends Activity {
	private EventsData events;
	private SQLiteDatabase db;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

	    Bundle extras = getIntent().getExtras(); 
	    String title = extras.getString("title"); //get event title from extras bundle
	    
	    setContentView(R.layout.event_date);
	    
	    events = new EventsData(this);
	    db=events.getReadableDatabase();
			Cursor event = db.query("events", new String[] {"title, date_start, date_end, sponsorship_deadline, signup_start, signup_end"}, 
	                "title LIKE '" + title +"' LIMIT 1", null, null, null, null);
			startManagingCursor(event);
		
		TextView startDate = (TextView)this.findViewById(R.id.start_date);	
		TextView endDate = (TextView)this.findViewById(R.id.end_date);	
		TextView signupStart = (TextView)this.findViewById(R.id.signup_start_date);			
		TextView signupEnd = (TextView)this.findViewById(R.id.signup_end_date);	
		TextView sponDead = (TextView)this.findViewById(R.id.spon_dead);	
		
		try {
			Date currDate = new Date();
	        
			if (event.moveToFirst()){
				DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
				DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
				
				Date dateStart = new Date(event.getLong(event.getColumnIndex("date_start"))); //Android works in milliseconds since 1/1/1970, not seconds
				Date dateEnd = new Date(event.getLong(event.getColumnIndex("date_end")));
				Date dateSignupStart = new Date(event.getLong(event.getColumnIndex("signup_start")));
				Date dateSignupEnd = new Date(event.getLong(event.getColumnIndex("signup_end")));
				Date dateSponDead = new Date(event.getLong(event.getColumnIndex("sponsorship_deadline")));

				if(currDate.compareTo(dateStart) <= 0){
					startDate.setText(dateFormat.format(dateStart)+" at "+ timeFormat.format(dateStart));
					endDate.setText(dateFormat.format(dateEnd)+" at "+ timeFormat.format(dateEnd));
				} else {
					findViewById(R.id.event_date_wrapper).setVisibility(View.GONE);
				}
				
				if(currDate.compareTo(dateSignupStart) <= 0){
					signupStart.setText(dateFormat.format(dateSignupStart)+" at "+ timeFormat.format(dateSignupStart));
					signupEnd.setText(dateFormat.format(dateSignupEnd)+" at "+ timeFormat.format(dateSignupEnd));
				} else {
					findViewById(R.id.signup_date_wrapper).setVisibility(View.GONE);
				}
				if(currDate.compareTo(dateSponDead) <= 0){
					sponDead.setText(dateFormat.format(dateSponDead)+" at "+ timeFormat.format(dateSponDead));
				} else {
					findViewById(R.id.spon_date_wrapper).setVisibility(View.GONE);
				}
			}

		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.toString(),
     	              Toast.LENGTH_LONG).show();	 
		}
		event.close();
    }
    
	
    public void onDestroy(){
    	super.onDestroy();
    	db.close(); //Close DB to prevent memory leaks
    }
    
}
